<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use User\Api\UserApi;

/** 
 * 后台用户控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class FinanceController extends AdminController {

    /**
     * 用户管理首页 
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function index(){
       	
		$productModel = M('finance');
		$map['is_delete'] = 0;
		$da = 5;  
        $member=M('member')->select();  
        $category=M('category')->select();  
		
        if (IS_POST) {
            // 财务记录用户查询
            $username= I('username');
            $map['username']=  array('like','%'.$username.'%'); 
        }
		
		$page=new \Think\Page($productModel->where($map)->count(),$da); //分页显示 
		$page->setConfig('theme','%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%'); 
		$finance = $productModel
        ->where($map)
        ->order('time desc,classes')
        // ->field('id,classes,username,quantity,name,time,total,unit_price')
		->limit($page->firstRow . ',' . $page->listRows) 
        ->select(); 
        foreach($finance as $key=>&$value){
            $value['time'] = date('Y-m-d',$value['time']);
        }    
        // print_r($finance);die;    
        $this->assign('member', $member);
        $this->assign('category', $category);
        $this->assign('_finance', $finance); 
        $this->meta_title = '用户信息'; 
	    $this->assign('_page', $page->show());  // 赋值分页输出 
        $this->display();
    }

    // 财务记录用户增加

    public function addfinance(){ 

        if (IS_POST) { 
            $li = I('post.li'); 
            $li['unit_price'] = M('category')->where(['name'=>$li['name']])->getField('unit'); 
            $li['time'] =  strtotime($li['time']);  
            $li['total'] = $li['quantity'] * $li['unit_price'];    // 计算总价
            $flag = M('finance')->add($li); 
            $li['id'] = $flag;
            $li['time'] = date('Y-m-d',$li['time']);
            !empty($flag)&&$this->ajaxReturn($li,'JSON');print_r(I('post.')); 
        } 
        $this->assign('category', $category);
    }
    // 财务记录用户删除
    public function delfinance()
    {	
    	$id = I('id'); 	
		$data['is_delete']=1; 
		$re = M('finance')->where(array('id'=>$id))->save($data);
        
		if($re == true){
			$this->success('删除成功');
		}
		else{
			$this->error('删除失败');
		} 
    } 

    // 财务记录下分类定价模块
    public function category()
    {   
        $finance=M('finance')->select();  
    	$productModel = M('category');
		$map['is_delete'] = 0;
		$da = 5; 
        if (IS_POST) {
        	// 分类定价名称查询 
            $username = I('name');
            $map['name']=  array('like','%'.$username.'%');
        }   
		$page=new \Think\Page($productModel->where($map)->count(),$da); //分页显示 
		$page->setConfig('theme','%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
       	$category = $productModel
        ->where($map)
		->limit($page->firstRow . ',' . $page->listRows) 
        ->select();  
	   
        foreach($category as $key=>&$value){
            $value['create_time'] = date('Y-m-d',$value['create_time']);
        }
        $this->assign('finance', $finance); 
        $this->assign('_category', $category);
		$this->assign('_page', $page->show());  // 赋值分页输出
        $this->meta_title = '用户信息';
        $this->display();
    }
   // 分类定价用户增加 
    public function addcategory(){ 
        if (IS_POST) { 
            $li = I('post.li');
            
            $li['create_time'] = time(); 
            $flag = M('category')->add($li);
            $li['create_time'] = date('Y-m-d',$li['create_time']);
            $li['id'] = $flag; 
            !empty($flag)&&$this->ajaxReturn($li,'JSON');
        } 
    } 
    // 分类定价用户删除
   public function delcategory()
    {   
        $id = I('id');  
        $data['is_delete']=1; 
        $re = M('category')->where(array('id'=>$id))->save($data); 
        if($re == true){
            $this->success('删除成功');
        }
        else{
            $this->error('删除失败');
        }
    }

}
